<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>你知道Java8为什么要引入Lambda表达式吗？ | 冰河技术</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/favicon.ico">
    <script charset="utf-8" async="async" src="/js/jquery.min.js"></script>
    <script charset="utf-8" async="async" src="/js/global.js"></script>
    <script charset="utf-8" async="async" src="/js/fingerprint2.min.js"></script>
    <script charset="utf-8" async="async" src="https://v1.cnzz.com/z_stat.php?id=1281063564&amp;web_id=1281063564"></script>
    <script charset="utf-8" async="async" src="https://s9.cnzz.com/z_stat.php?id=1281064551&amp;web_id=1281064551"></script>
    <script>
            var _hmt = _hmt || [];
            (function() {
              var hm = document.createElement("script");
              hm.src = "https://hm.baidu.com/hm.js?d091d2fd0231588b1d0f9231e24e3f5e";
              var s = document.getElementsByTagName("script")[0];
              s.parentNode.insertBefore(hm, s);
            })();
            </script>
    <meta name="description" content="包含：编程语言，开发技术，分布式，微服务，高并发，高可用，高可扩展，高可维护，JVM技术，MySQL，分布式数据库，分布式事务，云原生，大数据，云计算，渗透技术，各种面试题，面试技巧...">
    <meta property="article:modified_time" content="2022-05-23T11:30:51.000Z">
    <meta property="og:title" content="你知道Java8为什么要引入Lambda表达式吗？">
    <meta property="og:type" content="article">
    <meta property="og:url" content="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html">
    <meta name="twitter:title" content="你知道Java8为什么要引入Lambda表达式吗？">
    <meta name="twitter:url" content="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html">
    <meta name="twitter:card" content="summary_large_image">
    <meta name="robots" content="all">
    <meta name="author" content="冰河">
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <meta name="keywords" content="冰河，冰河技术, 编程语言，开发技术，分布式，微服务，高并发，高可用，高可扩展，高可维护，JVM技术，MySQL，分布式数据库，分布式事务，云原生，大数据，云计算，渗透技术，各种面试题，面试技巧">
    <meta name="apple-mobile-web-app-capable" content="yes">
    
    <link rel="preload" href="/assets/css/0.styles.ab888ebb.css" as="style"><link rel="preload" href="/assets/css/styles.css?v=1653305936337" as="style"><link rel="preload" href="/assets/js/cg-styles.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-app.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-4.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-3.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-49.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-5.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-6.js?v=1653305936337" as="script">
    <link rel="stylesheet" href="/assets/css/0.styles.ab888ebb.css"><link rel="stylesheet" href="/assets/css/styles.css?v=1653305936337">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">冰河技术</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/md/other/guide-to-reading.html" class="nav-link">
  导读
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="核心技术" class="dropdown-title"><span class="title">核心技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          Java核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/java/basics/2022-04-28-全网最全正则表达式总结.html" class="nav-link">
  Java基础
</a></li><li class="dropdown-subitem"><a href="/md/core/java/advanced/default.html" class="nav-link">
  Java进阶
</a></li><li class="dropdown-subitem"><a href="/md/core/java/senior/default.html" class="nav-link">
  Java高级
</a></li><li class="dropdown-subitem"><a href="/md/core/java/java8/2022-03-31-001-Java8有哪些新特性呢？.html" class="nav-link">
  Java8新特性
</a></li></ul></li><li class="dropdown-item"><h4>
          Spring核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/spring/ioc/2022-04-04-001-聊聊Spring注解驱动开发那些事儿.html" class="nav-link">
  IOC核心技术
</a></li><li class="dropdown-subitem"><a href="/md/core/spring/aop/default.html" class="nav-link">
  AOP核心技术
</a></li></ul></li><li class="dropdown-item"><h4>
          JVM核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/jvm/2022-04-18-001-JVM调优的几种场景.html" class="nav-link">
  JVM调优技术
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="性能调优" class="dropdown-title"><span class="title">性能调优</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/performance/jvm/default.html" class="nav-link">
  JVM性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/tomcat/default.html" class="nav-link">
  Tomcat性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/mysql/default.html" class="nav-link">
  MySQL性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/system/default.html" class="nav-link">
  操作系统性能调优
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="并发编程" class="dropdown-title"><span class="title">并发编程</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/concurrent/bottom/default.html" class="nav-link">
  底层技术
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/source/2020-03-30-001-一文搞懂线程与多线程.html" class="nav-link">
  源码分析
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/basics/2020-03-30-001-明明中断了线程，却为何不起作用呢？.html" class="nav-link">
  基础案例
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/ActualCombat/default.html" class="nav-link">
  实战案例
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/interview/default.html" class="nav-link">
  面试
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/framework/default.html" class="nav-link">
  系统架构
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="框架源码" class="dropdown-title"><span class="title">框架源码</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/frame/spring/default.html" class="nav-link">
  Spring源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/springmvc/default.html" class="nav-link">
  SpringMVC源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/mybatis/default.html" class="nav-link">
  MyBatis源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/dubbo/default.html" class="nav-link">
  Dubbo源码
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="分布式" class="dropdown-title"><span class="title">分布式</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          缓存技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/cache/default.html" class="nav-link">
  Redis
</a></li></ul></li><li class="dropdown-item"><h4>
          服务注册发现
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/zookeeper/default.html" class="nav-link">
  Zookeeper
</a></li></ul></li><li class="dropdown-item"><h4>
          消息中间件
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/mq/rabbitmq/default.html" class="nav-link">
  RabbitMQ
</a></li><li class="dropdown-subitem"><a href="/md/distributed/mq/rocketmq/default.html" class="nav-link">
  RocketMQ
</a></li><li class="dropdown-subitem"><a href="/md/distributed/mq/kafka/default.html" class="nav-link">
  Kafka
</a></li></ul></li><li class="dropdown-item"><h4>
          网络通信
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/netty/default.html" class="nav-link">
  Netty
</a></li></ul></li><li class="dropdown-item"><h4>
          远程调用
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/dubbo/default.html" class="nav-link">
  Dubbo
</a></li></ul></li><li class="dropdown-item"><h4>
          数据库
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/mongodb/default.html" class="nav-link">
  MongoDB
</a></li></ul></li><li class="dropdown-item"><h4>
          搜索引擎
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/es/default.html" class="nav-link">
  ElasticSearch
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="微服务" class="dropdown-title"><span class="title">微服务</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/microservices/springboot/default.html" class="nav-link">
  SpringBoot
</a></li><li class="dropdown-item"><!----> <a href="/md/microservices/springcloudalibaba/2022-04-02-SpringCloudAlibaba专栏开篇.html" class="nav-link">
  SpringCloudAlibaba
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="中间件" class="dropdown-title"><span class="title">中间件</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/middleware/bytecode/2022-04-11-001-工作多年的你依然重复做着CRUD-是否接触过这种技术.html" class="nav-link">
  字节码编程
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/threadpool/default.html" class="nav-link">
  手写线程池
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/limiter/default.html" class="nav-link">
  分布式限流
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/independent/default.html" class="nav-link">
  开源项目
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="项目实战" class="dropdown-title"><span class="title">项目实战</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/microservices/springcloudalibaba/2022-04-02-SpringCloudAlibaba专栏开篇.html" class="nav-link">
  SpringCloud Alibaba实战
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="渗透技术" class="dropdown-title"><span class="title">渗透技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/hack/environment/2022-04-17-001-安装Kali系统.html" class="nav-link">
  基础环境篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/tools/2022-04-17-001-使用Easy-Creds工具攻击无线网络.html" class="nav-link">
  渗透工具篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/horse/2022-05-02-001-各种一句话木马大全.html" class="nav-link">
  木马篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/sql/2022-05-02-001-sqli-labs-master下载与安装.html" class="nav-link">
  SQL注入篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/shell/2022-05-02-001-各种解析漏洞拿shell.html" class="nav-link">
  漏洞拿Shell篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/crack/2022-05-02-001-使用rarcrack暴力破解RAR-ZIP-7Z压缩包.html" class="nav-link">
  暴力破解篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/bash/2022-05-02-001-3389脚本开启代码(vbs版).html" class="nav-link">
  渗透脚本篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/raising/2022-05-02-001-数据库提权.html" class="nav-link">
  数据与系统提权篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/client/2022-05-02-001-浏览器渗透.html" class="nav-link">
  客户端渗透篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/sociology/2022-05-02-001-Metasploit之社会工程学工具包.html" class="nav-link">
  社会工程学
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/question/2022-05-02-001-HTTP错误4031禁止访问-执行访问被拒绝.html" class="nav-link">
  问题记录篇
</a></li></ul></div></div><div class="nav-item"><a href="/md/interview/2022-04-18-001-面试必问-聊聊JVM性能调优.html" class="nav-link">
  面试必问系列
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="📚PDF" class="dropdown-title"><span class="title">📚PDF</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          出版图书
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-深入理解分布式事务.html" class="nav-link">
  《深入理解分布式事务：原理与实战》
</a></li><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-MySQL技术大全.html" class="nav-link">
  《MySQL技术大全：开发、优化与运维实战》
</a></li><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-海量数据处理与大数据技术实战.html" class="nav-link">
  《海量数据处理与大数据技术实战》
</a></li></ul></li><li class="dropdown-item"><h4>
          电子书籍
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/knowledge/pdf/2022-03-30-《冰河的渗透实战笔记》电子书，442页，37万字，正式发布.html" class="nav-link">
  冰河的渗透实战笔记
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="关于" class="dropdown-title"><span class="title">关于</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/about/me/about-me.html" class="nav-link">
  关于自己
</a></li><li class="dropdown-item"><!----> <a href="/md/about/study/default.html" class="nav-link">
  关于学习
</a></li><li class="dropdown-item"><!----> <a href="/md/about/job/default.html" class="nav-link">
  关于职场
</a></li></ul></div></div><div class="nav-item"><a href="https://space.bilibili.com/517638832" target="_blank" rel="noopener noreferrer" class="nav-link external">
  B站
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://github.com/binghe001/BingheGuide" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/md/other/guide-to-reading.html" class="nav-link">
  导读
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="核心技术" class="dropdown-title"><span class="title">核心技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          Java核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/java/basics/2022-04-28-全网最全正则表达式总结.html" class="nav-link">
  Java基础
</a></li><li class="dropdown-subitem"><a href="/md/core/java/advanced/default.html" class="nav-link">
  Java进阶
</a></li><li class="dropdown-subitem"><a href="/md/core/java/senior/default.html" class="nav-link">
  Java高级
</a></li><li class="dropdown-subitem"><a href="/md/core/java/java8/2022-03-31-001-Java8有哪些新特性呢？.html" class="nav-link">
  Java8新特性
</a></li></ul></li><li class="dropdown-item"><h4>
          Spring核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/spring/ioc/2022-04-04-001-聊聊Spring注解驱动开发那些事儿.html" class="nav-link">
  IOC核心技术
</a></li><li class="dropdown-subitem"><a href="/md/core/spring/aop/default.html" class="nav-link">
  AOP核心技术
</a></li></ul></li><li class="dropdown-item"><h4>
          JVM核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/jvm/2022-04-18-001-JVM调优的几种场景.html" class="nav-link">
  JVM调优技术
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="性能调优" class="dropdown-title"><span class="title">性能调优</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/performance/jvm/default.html" class="nav-link">
  JVM性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/tomcat/default.html" class="nav-link">
  Tomcat性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/mysql/default.html" class="nav-link">
  MySQL性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/system/default.html" class="nav-link">
  操作系统性能调优
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="并发编程" class="dropdown-title"><span class="title">并发编程</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/concurrent/bottom/default.html" class="nav-link">
  底层技术
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/source/2020-03-30-001-一文搞懂线程与多线程.html" class="nav-link">
  源码分析
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/basics/2020-03-30-001-明明中断了线程，却为何不起作用呢？.html" class="nav-link">
  基础案例
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/ActualCombat/default.html" class="nav-link">
  实战案例
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/interview/default.html" class="nav-link">
  面试
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/framework/default.html" class="nav-link">
  系统架构
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="框架源码" class="dropdown-title"><span class="title">框架源码</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/frame/spring/default.html" class="nav-link">
  Spring源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/springmvc/default.html" class="nav-link">
  SpringMVC源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/mybatis/default.html" class="nav-link">
  MyBatis源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/dubbo/default.html" class="nav-link">
  Dubbo源码
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="分布式" class="dropdown-title"><span class="title">分布式</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          缓存技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/cache/default.html" class="nav-link">
  Redis
</a></li></ul></li><li class="dropdown-item"><h4>
          服务注册发现
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/zookeeper/default.html" class="nav-link">
  Zookeeper
</a></li></ul></li><li class="dropdown-item"><h4>
          消息中间件
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/mq/rabbitmq/default.html" class="nav-link">
  RabbitMQ
</a></li><li class="dropdown-subitem"><a href="/md/distributed/mq/rocketmq/default.html" class="nav-link">
  RocketMQ
</a></li><li class="dropdown-subitem"><a href="/md/distributed/mq/kafka/default.html" class="nav-link">
  Kafka
</a></li></ul></li><li class="dropdown-item"><h4>
          网络通信
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/netty/default.html" class="nav-link">
  Netty
</a></li></ul></li><li class="dropdown-item"><h4>
          远程调用
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/dubbo/default.html" class="nav-link">
  Dubbo
</a></li></ul></li><li class="dropdown-item"><h4>
          数据库
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/mongodb/default.html" class="nav-link">
  MongoDB
</a></li></ul></li><li class="dropdown-item"><h4>
          搜索引擎
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/es/default.html" class="nav-link">
  ElasticSearch
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="微服务" class="dropdown-title"><span class="title">微服务</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/microservices/springboot/default.html" class="nav-link">
  SpringBoot
</a></li><li class="dropdown-item"><!----> <a href="/md/microservices/springcloudalibaba/2022-04-02-SpringCloudAlibaba专栏开篇.html" class="nav-link">
  SpringCloudAlibaba
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="中间件" class="dropdown-title"><span class="title">中间件</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/middleware/bytecode/2022-04-11-001-工作多年的你依然重复做着CRUD-是否接触过这种技术.html" class="nav-link">
  字节码编程
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/threadpool/default.html" class="nav-link">
  手写线程池
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/limiter/default.html" class="nav-link">
  分布式限流
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/independent/default.html" class="nav-link">
  开源项目
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="项目实战" class="dropdown-title"><span class="title">项目实战</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/microservices/springcloudalibaba/2022-04-02-SpringCloudAlibaba专栏开篇.html" class="nav-link">
  SpringCloud Alibaba实战
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="渗透技术" class="dropdown-title"><span class="title">渗透技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/hack/environment/2022-04-17-001-安装Kali系统.html" class="nav-link">
  基础环境篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/tools/2022-04-17-001-使用Easy-Creds工具攻击无线网络.html" class="nav-link">
  渗透工具篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/horse/2022-05-02-001-各种一句话木马大全.html" class="nav-link">
  木马篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/sql/2022-05-02-001-sqli-labs-master下载与安装.html" class="nav-link">
  SQL注入篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/shell/2022-05-02-001-各种解析漏洞拿shell.html" class="nav-link">
  漏洞拿Shell篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/crack/2022-05-02-001-使用rarcrack暴力破解RAR-ZIP-7Z压缩包.html" class="nav-link">
  暴力破解篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/bash/2022-05-02-001-3389脚本开启代码(vbs版).html" class="nav-link">
  渗透脚本篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/raising/2022-05-02-001-数据库提权.html" class="nav-link">
  数据与系统提权篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/client/2022-05-02-001-浏览器渗透.html" class="nav-link">
  客户端渗透篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/sociology/2022-05-02-001-Metasploit之社会工程学工具包.html" class="nav-link">
  社会工程学
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/question/2022-05-02-001-HTTP错误4031禁止访问-执行访问被拒绝.html" class="nav-link">
  问题记录篇
</a></li></ul></div></div><div class="nav-item"><a href="/md/interview/2022-04-18-001-面试必问-聊聊JVM性能调优.html" class="nav-link">
  面试必问系列
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="📚PDF" class="dropdown-title"><span class="title">📚PDF</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          出版图书
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-深入理解分布式事务.html" class="nav-link">
  《深入理解分布式事务：原理与实战》
</a></li><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-MySQL技术大全.html" class="nav-link">
  《MySQL技术大全：开发、优化与运维实战》
</a></li><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-海量数据处理与大数据技术实战.html" class="nav-link">
  《海量数据处理与大数据技术实战》
</a></li></ul></li><li class="dropdown-item"><h4>
          电子书籍
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/knowledge/pdf/2022-03-30-《冰河的渗透实战笔记》电子书，442页，37万字，正式发布.html" class="nav-link">
  冰河的渗透实战笔记
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="关于" class="dropdown-title"><span class="title">关于</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/about/me/about-me.html" class="nav-link">
  关于自己
</a></li><li class="dropdown-item"><!----> <a href="/md/about/study/default.html" class="nav-link">
  关于学习
</a></li><li class="dropdown-item"><!----> <a href="/md/about/job/default.html" class="nav-link">
  关于职场
</a></li></ul></div></div><div class="nav-item"><a href="https://space.bilibili.com/517638832" target="_blank" rel="noopener noreferrer" class="nav-link external">
  B站
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://github.com/binghe001/BingheGuide" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Java基础</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/md/core/java/basics/2022-04-28-全网最全正则表达式总结.html" class="sidebar-link">全网最全正则表达式总结</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Java进阶</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/md/core/java/advanced/default.html" class="sidebar-link">关于我</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Java高级</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/md/core/java/senior/default.html" class="sidebar-link">关于我</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Java8新特性</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/md/core/java/java8/2022-03-31-001-Java8有哪些新特性呢？.html" class="sidebar-link">一张图带你领略Java8有哪些新特性</a></li><li><a href="/md/core/java/java8/2022-03-31-002-你知道Java8为什么引入Lambda表达式吗.html" class="active sidebar-link">你知道Java8为什么要引入Lambda表达式吗？</a></li><li><a href="/md/core/java/java8/2022-03-31-003-Lambda表达式基础语法，都在这儿了.html" class="sidebar-link">Lambda表达式基础语法，都在这儿了！！</a></li><li><a href="/md/core/java/java8/2022-03-31-004-Lambda表达式典型案例，你想要的的都在这儿了.html" class="sidebar-link">Lambda表达式典型案例，你想要的的都在这儿了！！</a></li><li><a href="/md/core/java/java8/2022-03-31-005-一文搞懂函数式接口.html" class="sidebar-link">一文搞懂函数式接口</a></li><li><a href="/md/core/java/java8/2022-03-31-006-知识点总结，你都会了吗.html" class="sidebar-link">知识点总结，你都会了吗？</a></li><li><a href="/md/core/java/java8/2022-03-31-007-方法引用和构造器引用.html" class="sidebar-link">浅谈方法引用和构造器引用</a></li><li><a href="/md/core/java/java8/2022-03-31-008-关于Java8的Stream API,都在这儿了.html" class="sidebar-link">关于Java8的Stream API,都在这儿了</a></li><li><a href="/md/core/java/java8/2022-03-31-009-强大的Stream API，你了解吗.html" class="sidebar-link">强大的Stream API，你了解吗</a></li><li><a href="/md/core/java/java8/2022-03-31-010-Stream API有哪些中间操作,看完你也可以吊打面试官.html" class="sidebar-link">Stream API有哪些中间操作？看完你也可以吊打面试官！！</a></li><li><a href="/md/core/java/java8/2022-03-31-011-Java8中的Stream API有哪些终止操作.html" class="sidebar-link">Java8中的Stream API有哪些终止操作</a></li><li><a href="/md/core/java/java8/2022-03-31-012-关于并行流与串行流，你必须掌握这些.html" class="sidebar-link">关于并行流与串行流，你必须掌握这些！！</a></li><li><a href="/md/core/java/java8/2022-03-31-013-不了解Optional类，简历上别说你懂Java8.html" class="sidebar-link">不了解Optional类，简历上别说你懂Java8</a></li><li><a href="/md/core/java/java8/2022-03-31-014-接口中的默认方法和静态方法，你都掌握了吗.html" class="sidebar-link">接口中的默认方法和静态方法，你都掌握了吗？</a></li><li><a href="/md/core/java/java8/2022-03-31-015-关于Java8中的日期时间API，你需要掌握这些.html" class="sidebar-link">关于Java8中的日期时间API，你需要掌握这些！！</a></li><li><a href="/md/core/java/java8/2022-03-31-016-重复注解与类型注解，你真的学会了吗.html" class="sidebar-link">重复注解与类型注解，你真的学会了吗？</a></li><li><a href="/md/core/java/java8/2022-04-01-001-Java8新特性总结.html" class="sidebar-link">Java8新特性总结</a></li></ul></section></li></ul> </aside> <div><main class="page"> <div class="theme-default-content content__default"><h1 id="你知道java8为什么要引入lambda表达式吗"><a href="#你知道java8为什么要引入lambda表达式吗" class="header-anchor">#</a> 你知道Java8为什么要引入Lambda表达式吗？</h1> <h2 id="写在前面"><a href="#写在前面" class="header-anchor">#</a> 写在前面</h2> <blockquote><p>这是一道真实的面试题，一个读者朋友出去面试，面试官竟然问他这样一个问题：你说说Java8中为什么引入Lambda表达式？引入Lambda表达式后有哪些好处呢？还好这个朋友对Java8早有准备。不过，如果是看文章的你出去面试，面试官问你这样的问题，你是否也能轻松回答呢？</p></blockquote> <h2 id="什么是lambda表达式"><a href="#什么是lambda表达式" class="header-anchor">#</a> 什么是Lambda表达式？</h2> <p>Lambda表达式是一个匿名函数，我们可以这样理解Lambda表达式：Lambda是一段可以传递的代码（能够做到将代码像数据一样进行传递）。使用Lambda表达式能够写出更加简洁、灵活的代码。并且，使用Lambda表达式能够使Java的语言表达能力得到提升。</p> <h2 id="匿名内部类"><a href="#匿名内部类" class="header-anchor">#</a> 匿名内部类</h2> <p>在介绍如何使用Lambda表达式之前，我们先来看看匿名内部类，例如，我们使用匿名内部类比较两个Integer类型数据的大小。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">Comparator</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> com <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Comparator</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">compare</span><span class="token punctuation">(</span><span class="token class-name">Integer</span> o1<span class="token punctuation">,</span> <span class="token class-name">Integer</span> o2<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">compare</span><span class="token punctuation">(</span>o1<span class="token punctuation">,</span> o2<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>在上述代码中，我们使用匿名内部类实现了比较两个Integer类型数据的大小。</p> <p>接下来，我们就可以将上述匿名内部类的实例作为参数，传递到其他方法中了，如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code> <span class="token class-name">TreeSet</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> treeSet <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TreeSet</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span>com<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>完整的代码如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">Comparator</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> com <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Comparator</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token annotation punctuation">@Override</span>
        <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">compare</span><span class="token punctuation">(</span><span class="token class-name">Integer</span> o1<span class="token punctuation">,</span> <span class="token class-name">Integer</span> o2<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">compare</span><span class="token punctuation">(</span>o1<span class="token punctuation">,</span> o2<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token class-name">TreeSet</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> treeSet <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TreeSet</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span>com<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>我们分析下上述代码，在整个匿名内部类中，实际上真正有用的就是下面一行代码。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code> <span class="token keyword">return</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">compare</span><span class="token punctuation">(</span>o1<span class="token punctuation">,</span> o2<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>其他的代码本质上都是“冗余”的。但是为了书写上面的一行代码，我们不得不在匿名内部类中书写更多的代码。</p> <h2 id="lambda表达式"><a href="#lambda表达式" class="header-anchor">#</a> Lambda表达式</h2> <p>如果使用Lambda表达式完成两个Integer类型数据的比较，我们该如何实现呢？</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">Comparator</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> com <span class="token operator">=</span> <span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">compare</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>看到没，使用Lambda表达式，我们只需要使用一行代码就能够实现两个Integer类型数据的比较。</p> <p>我们也可以将Lambda表达式传递到TreeSet的构造方法中，如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code> <span class="token class-name">TreeSet</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> treeSet <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TreeSet</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">compare</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>直观的感受就是使用Lambda表达式一行代码就能搞定匿名内部类多行代码的功能。</p> <p><strong>看到这，不少读者会问：我使用匿名内部类的方式实现比较两个整数类型的数据大小并不复杂啊！我为啥还要学习一种新的语法呢？</strong></p> <p><img alt="" data-src="https://binghe001.github.io/images/java/java8/2022-03-31-002-001.jpg" loading="lazy" class="lazy"></p> <p>其实，我想说的是：上面咱们只是简单的列举了一个示例，接下来，咱们写一个稍微复杂一点的例子，来对比下使用匿名内部类与Lambda表达式哪种方式更加简洁。</p> <h2 id="对比常规方法和lambda表达式"><a href="#对比常规方法和lambda表达式" class="header-anchor">#</a> 对比常规方法和Lambda表达式</h2> <p>例如，现在有这样一个需求：<strong>获取当前公司中员工年龄大于30岁的员工信息。</strong></p> <p>首先，我们需要创建一个Employee实体类来存储员工的信息。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Data</span>
<span class="token annotation punctuation">@Builder</span>
<span class="token annotation punctuation">@ToString</span>
<span class="token annotation punctuation">@NoArgsConstructor</span>
<span class="token annotation punctuation">@AllArgsConstructor</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Employee</span> <span class="token keyword">implements</span> <span class="token class-name">Serializable</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token keyword">long</span> serialVersionUID <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">9079722457749166858L</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">Integer</span> age<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">Double</span> salary<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>在Employee中，我们简单存储了员工的姓名、年龄和薪资。</p> <p>接下来，我们创建一个存储多个员工的List集合，如下所示。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>protected List<span class="token operator">&lt;</span>Employee<span class="token operator">&gt;</span> employees <span class="token operator">=</span> Arrays.asList<span class="token punctuation">(</span>
		new Employee<span class="token punctuation">(</span><span class="token string">&quot;张三&quot;</span>, <span class="token number">18</span>, <span class="token number">9999.99</span><span class="token punctuation">)</span>,
		new Employee<span class="token punctuation">(</span><span class="token string">&quot;李四&quot;</span>, <span class="token number">38</span>, <span class="token number">5555.55</span><span class="token punctuation">)</span>,
		new Employee<span class="token punctuation">(</span><span class="token string">&quot;王五&quot;</span>, <span class="token number">60</span>, <span class="token number">6666.66</span><span class="token punctuation">)</span>,
		new Employee<span class="token punctuation">(</span><span class="token string">&quot;赵六&quot;</span>, <span class="token number">16</span>, <span class="token number">7777.77</span><span class="token punctuation">)</span>,
		new Employee<span class="token punctuation">(</span><span class="token string">&quot;田七&quot;</span>, <span class="token number">18</span>, <span class="token number">3333.33</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="_1-常规遍历集合"><a href="#_1-常规遍历集合" class="header-anchor">#</a> 1.常规遍历集合</h3> <p>我们先使用常规遍历集合的方式来查找年龄大于等于30的员工信息。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span> <span class="token function">filterEmployeesByAge</span><span class="token punctuation">(</span><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span> list<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span> employees <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token class-name">Employee</span> e <span class="token operator">:</span> list<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span><span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;=</span> <span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            employees<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> employees<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>接下来，我们测试一下上面的方法。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test3</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span> employeeList <span class="token operator">=</span> <span class="token function">filterEmployeesByAge</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>employees<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Employee</span> e <span class="token operator">:</span> employeeList<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>运行test3方法，输出信息如下所示。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>李四, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">38</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">5555.55</span><span class="token punctuation">)</span>
Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>王五, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">60</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">6666.66</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>总体来说，查找年龄大于或者等于30的员工信息，使用常规遍历集合的方式稍显复杂了。</p> <p>例如，<strong>需求发生了变化：获取当前公司中员工工资大于或者等于5000的员工信息。</strong></p> <p>此时，我们不得不再次创建一个按照工资过滤的方法。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span> <span class="token function">filterEmployeesBySalary</span><span class="token punctuation">(</span><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span> list<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span> employees <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token class-name">Employee</span> e <span class="token operator">:</span> list<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;=</span> <span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            employees<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> employees<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>对比filterEmployeesByAge()方法和filterEmployeesBySalary方法后，我们发现，大部分的方法体是相同的，只是for循环中对于条件的判断不同。</p> <p>如果此时我们再来一个需求，<strong>查找当前公司中年龄小于或者等于20的员工信息，那我们又要创建一个过滤方法了。</strong> 看来使用常规方法是真的不方便啊！</p> <p><img alt="" data-src="https://binghe001.github.io/images/java/java8/2022-03-31-002-002.jpg" loading="lazy" class="lazy"></p> <p>这里，问大家一个问题：对于这种常规方法最好的优化方式是啥？相信有不少小伙伴会说：将公用的方法抽取出来。没错，将公用的方法抽取出来是一种优化方式，但它不是最好的方式。最好的方式是啥？那就是使用 <strong>设计模式</strong> 啊！设计模式可是无数前辈不断实践而总结出的设计原则和设计模式。大家可以查看《<a href="https://mp.weixin.qq.com/s?__biz=Mzg3MzE1NTIzNA==&amp;mid=2247483964&amp;idx=1&amp;sn=918d33f1b424926cafff5d8aef36550d&amp;chksm=cee51df1f99294e7fbd0166764a3563d77b3e8f4dfa297eeea2d9d8ca347ce7676685fafb228&amp;token=598200942&amp;lang=zh_CN#rd" target="_blank" rel="noopener noreferrer">设计模式汇总——你需要掌握的23种设计模式都在这儿了！<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>》一文来学习设计模式专题。</p> <h3 id="_2-使用设计模式优化代码"><a href="#_2-使用设计模式优化代码" class="header-anchor">#</a> 2.使用设计模式优化代码</h3> <p>如何使用设计模式来优化上面的方法呢，大家继续往下看，对于设计模式不熟悉的同学可以先根据《<a href="https://mp.weixin.qq.com/s?__biz=Mzg3MzE1NTIzNA==&amp;mid=2247483964&amp;idx=1&amp;sn=918d33f1b424926cafff5d8aef36550d&amp;chksm=cee51df1f99294e7fbd0166764a3563d77b3e8f4dfa297eeea2d9d8ca347ce7676685fafb228&amp;token=598200942&amp;lang=zh_CN#rd" target="_blank" rel="noopener noreferrer">设计模式汇总——你需要掌握的23种设计模式都在这儿了！<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>》来学习。</p> <p>我们先定义一个泛型接口MyPredicate，对传递过来的数据进行过滤，符合规则返回true，不符合规则返回false。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">MyPredicate</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>

    <span class="token comment">/**
     * 对传递过来的T类型的数据进行过滤
     * 符合规则返回true，不符合规则返回false
     */</span>
    <span class="token keyword">boolean</span> <span class="token function">filter</span><span class="token punctuation">(</span><span class="token class-name">T</span> t<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>接下来，我们创建MyPredicate接口的实现类FilterEmployeeByAge来过滤年龄大于或者等于30的员工信息。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">FilterEmployeeByAge</span> <span class="token keyword">implements</span> <span class="token class-name">MyPredicate</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">filter</span><span class="token punctuation">(</span><span class="token class-name">Employee</span> employee<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> employee<span class="token punctuation">.</span><span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;=</span> <span class="token number">30</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>我们定义一个过滤员工信息的方法，此时传递的参数不仅有员工的信息集合，同时还有一个我们定义的接口实例，在遍历员工集合时将符合过滤条件的员工信息返回。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">//优化方式一</span>
<span class="token keyword">public</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span> <span class="token function">filterEmployee</span><span class="token punctuation">(</span><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span> list<span class="token punctuation">,</span> <span class="token class-name">MyPredicate</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span> myPredicate<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span> employees <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token class-name">Employee</span> e <span class="token operator">:</span> list<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>myPredicate<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            employees<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> employees<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>接下来，我们写一个测试方法来测试优化后的代码。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test4</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span> employeeList <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">filterEmployee</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>employees<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">FilterEmployeeByAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Employee</span> e <span class="token operator">:</span> employeeList<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>运行test4()方法，输出的结果信息如下所示。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>李四, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">38</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">5555.55</span><span class="token punctuation">)</span>
Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>王五, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">60</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">6666.66</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>写到这里，大家是否有一种豁然开朗的感觉呢？</p> <p><img alt="" data-src="https://binghe001.github.io/images/java/java8/2022-03-31-002-003.jpg" loading="lazy" class="lazy"></p> <p>没错，这就是设计模式的魅力，对于设计模式不熟悉的小伙伴，一定要参照《<a href="https://mp.weixin.qq.com/s?__biz=Mzg3MzE1NTIzNA==&amp;mid=2247483964&amp;idx=1&amp;sn=918d33f1b424926cafff5d8aef36550d&amp;chksm=cee51df1f99294e7fbd0166764a3563d77b3e8f4dfa297eeea2d9d8ca347ce7676685fafb228&amp;token=598200942&amp;lang=zh_CN#rd" target="_blank" rel="noopener noreferrer">设计模式汇总——你需要掌握的23种设计模式都在这儿了！<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>》来学习。</p> <p>我们继续获取当前公司中工资大于或者等于5000的员工信息，此时，我们只需要创建一个FilterEmployeeBySalary类实现MyPredicate接口，如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">FilterEmployeeBySalary</span> <span class="token keyword">implements</span> <span class="token class-name">MyPredicate</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">filter</span><span class="token punctuation">(</span><span class="token class-name">Employee</span> employee<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> employee<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;=</span> <span class="token number">5000</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>接下来，就可以直接写测试方法了，在测试方法中继续调用<code>filterEmployee(List&lt;Employee&gt; list, MyPredicate&lt;Employee&gt; myPredicate)</code>方法。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test5</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span> employeeList <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">filterEmployee</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>employees<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">FilterEmployeeBySalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Employee</span> e <span class="token operator">:</span> employeeList<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>运行test5方法，输出的结果信息如下所示。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>张三, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">18</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">9999.99</span><span class="token punctuation">)</span>
Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>李四, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">38</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">5555.55</span><span class="token punctuation">)</span>
Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>王五, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">60</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">6666.66</span><span class="token punctuation">)</span>
Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>赵六, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">16</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">7777.77</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>可以看到，使用设计模式对代码进行优化后，无论过滤员工信息的需求如何变化，我们只需要创建MyPredicate接口的实现类来实现具体的过滤逻辑，然后在测试方法中调用<code>filterEmployee(List&lt;Employee&gt; list, MyPredicate&lt;Employee&gt; myPredicate)</code>方法将员工集合和过滤规则传入即可。</p> <p><img alt="005" data-src="https://binghe001.github.io/images/java/java8/2022-03-31-002-004.jpg" loading="lazy" class="lazy"></p> <p>这里，问大家一个问题：上面优化代码使用的设计模式是哪种设计模式呢？如果是你，你会想到使用设计模式来优化自己的代码吗？小伙伴们自己先思考一下到底使用的设计模式是什么？<strong>文末我会给出答案！</strong></p> <p>使用设计模式优化代码也有不好的地方：每次定义一个过滤策略的时候，我们都要单独创建一个过滤类！！</p> <h3 id="_3-匿名内部类"><a href="#_3-匿名内部类" class="header-anchor">#</a> 3.匿名内部类</h3> <p>那使用匿名内部类是不是能够优化我们书写的代码呢，接下来，我们就使用匿名内部类来实现对员工信息的过滤。先来看过滤年龄大于或者等于30的员工信息。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test6</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span> employeeList <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">filterEmployee</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>employees<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">MyPredicate</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token annotation punctuation">@Override</span>
        <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">filter</span><span class="token punctuation">(</span><span class="token class-name">Employee</span> employee<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> employee<span class="token punctuation">.</span><span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;=</span> <span class="token number">30</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Employee</span> e <span class="token operator">:</span> employeeList<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>运行test6方法，输出如下结果信息。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>李四, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">38</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">5555.55</span><span class="token punctuation">)</span>
Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>王五, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">60</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">6666.66</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>再实现过滤工资大于或者等于5000的员工信息，如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test7</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span> employeeList <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">filterEmployee</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>employees<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">MyPredicate</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token annotation punctuation">@Override</span>
        <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">filter</span><span class="token punctuation">(</span><span class="token class-name">Employee</span> employee<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> employee<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;=</span> <span class="token number">5000</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Employee</span> e <span class="token operator">:</span> employeeList<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>运行test7方法，输出如下结果信息。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>张三, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">18</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">9999.99</span><span class="token punctuation">)</span>
Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>李四, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">38</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">5555.55</span><span class="token punctuation">)</span>
Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>王五, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">60</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">6666.66</span><span class="token punctuation">)</span>
Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>赵六, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">16</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">7777.77</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>匿名内部类看起来比常规遍历集合的方法要简单些，并且将使用设计模式优化代码时，每次创建一个类来实现过滤规则写到了匿名内部类中，使得代码进一步简化了。</p> <p>但是，使用匿名内部类代码的可读性不高，并且冗余代码也比较多！！</p> <p><strong>那还有没有更加简化的方式呢？</strong></p> <p><img alt="" data-src="https://binghe001.github.io/images/java/java8/2022-03-31-002-005.jpg" loading="lazy" class="lazy"></p> <h3 id="_4-重头戏-lambda表达式"><a href="#_4-重头戏-lambda表达式" class="header-anchor">#</a> 4.重头戏：Lambda表达式</h3> <p>在使用Lambda表达式时，我们还是要调用之前写的<code>filterEmployee(List&lt;Employee&gt; list, MyPredicate&lt;Employee&gt; myPredicate)</code>方法。</p> <p>注意看，获取年龄大于或者等于30的员工信息。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test8</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">filterEmployee</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>employees<span class="token punctuation">,</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token operator">-&gt;</span> e<span class="token punctuation">.</span><span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;=</span> <span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token operator">::</span><span class="token function">println</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>看到没，使用Lambda表达式只需要一行代码就完成了员工信息的过滤和输出。是不是很6呢。</p> <p><img alt="007" data-src="https://binghe001.github.io/images/java/java8/2022-03-31-002-006.jpg" loading="lazy" class="lazy"></p> <p>运行test8方法，输出如下的结果信息。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>李四, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">38</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">5555.55</span><span class="token punctuation">)</span>
Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>王五, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">60</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">6666.66</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>再来看使用Lambda表达式来获取工资大于或者等于5000的员工信息，如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test9</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">filterEmployee</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>employees<span class="token punctuation">,</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token operator">-&gt;</span> e<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;=</span> <span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token operator">::</span><span class="token function">println</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>没错，使用Lambda表达式，又是一行代码就搞定了！！</p> <p>运行test9方法，输出如下的结果信息。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>张三, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">18</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">9999.99</span><span class="token punctuation">)</span>
Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>李四, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">38</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">5555.55</span><span class="token punctuation">)</span>
Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>王五, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">60</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">6666.66</span><span class="token punctuation">)</span>
Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>赵六, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">16</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">7777.77</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>另外，使用Lambda表达式时，只需要给出需要过滤的集合，我们就能够实现从集合中过滤指定规则的元素，并输出结果信息。</p> <h3 id="_5-重头戏-stream-api"><a href="#_5-重头戏-stream-api" class="header-anchor">#</a> 5.重头戏：Stream API</h3> <p>使用Lambda表达式结合Stream API，只要给出相应的集合，我们就可以完成对集合的各种过滤并输出结果信息。</p> <p>例如，此时只要有一个<code>employees</code>集合，我们使用Lambda表达式来获取工资大于或者等于5000的员工信息。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test10</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    employees<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token operator">-&gt;</span> e<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;=</span> <span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token operator">::</span><span class="token function">println</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>没错，<strong>只给出一个集合，使用Lambda表达式和Stream API，一行代码就能够过滤出想要的元素并进行输出。</strong></p> <p>运行test10方法，输出如下的结果信息。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>张三, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">18</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">9999.99</span><span class="token punctuation">)</span>
Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>李四, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">38</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">5555.55</span><span class="token punctuation">)</span>
Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>王五, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">60</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">6666.66</span><span class="token punctuation">)</span>
Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>赵六, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">16</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">7777.77</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>如果我们只想要获取前两个员工的信息呢？其实也很简单，如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test11</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    employees<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token operator">-&gt;</span> e<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;=</span> <span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token operator">::</span><span class="token function">println</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>可以看到，我们在代码中添加了<code>limit(2)</code>来限制只获取两个员工信息。运行test11方法，输出如下的结果信息。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>张三, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">18</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">9999.99</span><span class="token punctuation">)</span>
Employee<span class="token punctuation">(</span>name<span class="token operator">=</span>李四, <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">38</span>, <span class="token assign-left variable">salary</span><span class="token operator">=</span><span class="token number">5555.55</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>使用Lambda表达式和Stream API也可以获取指定的字段信息，例如获取工资大于或者等于5000的员工姓名。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test12</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    employees<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token operator">-&gt;</span> e<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;=</span> <span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token class-name">Employee</span><span class="token operator">::</span><span class="token function">getName</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token operator">::</span><span class="token function">println</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>可以看到，使用map过滤出了工资大于或者等于5000的员工姓名。运行test12方法，输出如下的结果信息。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>张三
李四
王五
赵六
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>是不是很简单呢？</p> <p><img alt="" data-src="https://binghe001.github.io/images/java/java8/2022-03-31-002-007.jpg" loading="lazy" class="lazy"></p> <p>最后，给出文中使用的设计模式：<strong>策略模式。</strong></p> <h2 id="写在最后"><a href="#写在最后" class="header-anchor">#</a> 写在最后</h2> <blockquote><p>如果你觉得冰河写的还不错，请微信搜索并关注「 <strong>冰河技术</strong> 」微信公众号，跟冰河学习高并发、分布式、微服务、大数据、互联网和云原生技术，「 <strong>冰河技术</strong> 」微信公众号更新了大量技术专题，每一篇技术文章干货满满！不少读者已经通过阅读「 <strong>冰河技术</strong> 」微信公众号文章，吊打面试官，成功跳槽到大厂；也有不少读者实现了技术上的飞跃，成为公司的技术骨干！如果你也想像他们一样提升自己的能力，实现技术能力的飞跃，进大厂，升职加薪，那就关注「 <strong>冰河技术</strong> 」微信公众号吧，每天更新超硬核技术干货，让你对如何提升技术能力不再迷茫！</p></blockquote> <p><img alt="" data-src="https://img-blog.csdnimg.cn/20200906013715889.png" loading="lazy" class="lazy"></p></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/binghe001/BingheGuide/edit/master/docs/md/core/java/java8/2022-03-31-002-你知道Java8为什么引入Lambda表达式吗.md" target="_blank" rel="noopener noreferrer">在 GitHub 上编辑此页</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="last-updated"><span class="prefix">上次更新: </span> <span class="time">2022/5/23</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/md/core/java/java8/2022-03-31-001-Java8有哪些新特性呢？.html" class="prev">
          一张图带你领略Java8有哪些新特性
        </a></span> <span class="next"><a href="/md/core/java/java8/2022-03-31-003-Lambda表达式基础语法，都在这儿了.html">
          Lambda表达式基础语法，都在这儿了！！
        </a>
        →
      </span></p></div> </main></div> <aside class="page-sidebar"> <div class="page-side-toolbar"><div class="option-box-toc-fixed"><div class="toc-container-sidebar"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="max-height:650px"><div style="font-weight:bold;text-align:center;">你知道Java8为什么要引入Lambda表达式吗？</div> <hr> <div class="toc-box"><ul class="toc-sidebar-links"><li><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#写在前面" class="toc-sidebar-link">写在前面</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#什么是lambda表达式" class="toc-sidebar-link">什么是Lambda表达式？</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#匿名内部类" class="toc-sidebar-link">匿名内部类</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#lambda表达式" class="toc-sidebar-link">Lambda表达式</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#对比常规方法和lambda表达式" class="toc-sidebar-link">对比常规方法和Lambda表达式</a><ul class="toc-sidebar-sub-headers"><li class="toc-sidebar-sub-header"><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#_1-常规遍历集合" class="toc-sidebar-link">1.常规遍历集合</a></li><li class="toc-sidebar-sub-header"><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#_2-使用设计模式优化代码" class="toc-sidebar-link">2.使用设计模式优化代码</a></li><li class="toc-sidebar-sub-header"><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#_3-匿名内部类" class="toc-sidebar-link">3.匿名内部类</a></li><li class="toc-sidebar-sub-header"><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#_4-重头戏-lambda表达式" class="toc-sidebar-link">4.重头戏：Lambda表达式</a></li><li class="toc-sidebar-sub-header"><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#_5-重头戏-stream-api" class="toc-sidebar-link">5.重头戏：Stream API</a></li></ul></li><li><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#写在最后" class="toc-sidebar-link">写在最后</a><ul class="toc-sidebar-sub-headers"></ul></li></ul></div></div></div></div></div> <div class="option-box-toc-over"><img src="/images/system/toc.png" class="nozoom"> <span class="show-txt">目录</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="max-height:550px"><div style="font-weight:bold;text-align:center;">你知道Java8为什么要引入Lambda表达式吗？</div> <hr> <div class="toc-box"><ul class="toc-sidebar-links"><li><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#写在前面" class="toc-sidebar-link">写在前面</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#什么是lambda表达式" class="toc-sidebar-link">什么是Lambda表达式？</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#匿名内部类" class="toc-sidebar-link">匿名内部类</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#lambda表达式" class="toc-sidebar-link">Lambda表达式</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#对比常规方法和lambda表达式" class="toc-sidebar-link">对比常规方法和Lambda表达式</a><ul class="toc-sidebar-sub-headers"><li class="toc-sidebar-sub-header"><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#_1-常规遍历集合" class="toc-sidebar-link">1.常规遍历集合</a></li><li class="toc-sidebar-sub-header"><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#_2-使用设计模式优化代码" class="toc-sidebar-link">2.使用设计模式优化代码</a></li><li class="toc-sidebar-sub-header"><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#_3-匿名内部类" class="toc-sidebar-link">3.匿名内部类</a></li><li class="toc-sidebar-sub-header"><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#_4-重头戏-lambda表达式" class="toc-sidebar-link">4.重头戏：Lambda表达式</a></li><li class="toc-sidebar-sub-header"><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#_5-重头戏-stream-api" class="toc-sidebar-link">5.重头戏：Stream API</a></li></ul></li><li><a href="/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html#写在最后" class="toc-sidebar-link">写在最后</a><ul class="toc-sidebar-sub-headers"></ul></li></ul></div></div></div></div></div> <div class="option-box"><img src="/images/system/wechat.png" class="nozoom"> <span class="show-txt">手机看</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="text-align:center"><span style="font-size:0.9rem">微信扫一扫</span> <img height="180px" src="https://api.qrserver.com/v1/create-qr-code/?data=https://binghe001.github.io/md/core/java/java8/2022-03-31-002-%E4%BD%A0%E7%9F%A5%E9%81%93Java8%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BC%95%E5%85%A5Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%90%97.html" style="margin:10px;">
                可以<b>手机看</b>或分享至<b>朋友圈</b></div></div></div></div> <div class="option-box"><img src="/images/system/toggle.png" width="30px" class="nozoom"> <span class="show-txt">左栏</span></div> <div class="option-box"><img src="/images/system/xingqiu.png" width="25px" class="nozoom"> <span class="show-txt">星球</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="text-align:center"><span style="font-size:0.8rem;font-weight:bold;">实战项目<span style="font-size:8px;color:red;">「SpringCloud Alibaba实战项目」</span>、专属电子书、问题解答、简历指导、技术分享、晋升指导、视频课程</span> <img height="180px" src="/images/personal/xingqiu.png" style="margin:10px;"> <b>知识星球</b>：冰河技术
            </div></div></div></div> <div class="option-box"><img src="/images/system/wexin4.png" width="25px" class="nozoom"> <span class="show-txt">读者群</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="text-align:center"><span style="font-size:0.8rem;font-weight:bold;">添加冰河微信<span style="color:red;">(hacker_binghe)</span>进冰河技术学习交流圈「无任何套路」</span> <img src="/images/personal/hacker_binghe.jpg" height="180px" style="margin:10px;">
                PS：添加时请备注<b>读者加群</b>，谢谢！
              </div></div></div></div> <div class="option-box"><img src="/images/system/download-2.png" width="25px" class="nozoom"> <span class="show-txt">下资料</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="text-align:center"><span style="font-size:0.8rem;font-weight:bold;">扫描公众号，回复<span style="color:red;">“1024”</span>下载<span style="color:red;">100GB+</span>学习技术资料、PDF书籍、实战项目、简历模板等「无任何套路」</span> <img src="/images/personal/qrcode.png" height="180px" style="margin:10px;"> <b>公众号:</b> 冰河技术
              </div></div></div></div> <div class="option-box"><img src="/images/system/heart-1.png" width="25px" class="nozoom"> <span class="show-txt">赞赏我</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="text-align:center"><span style="font-size:0.8rem;font-weight:bold;">鼓励/支持/赞赏我</span> <img height="180px" src="/images/personal/encourage-head.png" style="margin:5px;"> <br>1. 不靠它生存但仍希望得到你的鼓励；
                <br>2. 时刻警醒自己保持技术人的初心；
              </div></div></div></div> <div title="一张图带你领略Java8有哪些新特性" class="option-box" style="padding-left:2px;text-align:center;"><a href="/md/core/java/java8/2022-03-31-001-Java8有哪些新特性呢？.html"><img src="/images/system/pre2.png" width="30px" class="nozoom"> <span class="show-txt">上一篇</span></a></div> <div title="Lambda表达式基础语法，都在这儿了！！" class="option-box" style="padding-left:2px;text-align:center;"><a href="/md/core/java/java8/2022-03-31-003-Lambda表达式基础语法，都在这儿了.html"><img src="/images/system/next2.png" width="30px" class="nozoom"> <span class="show-txt">下一篇</span></a></div></div>  <!----> </aside></div><div class="global-ui"><div class="read-more-wrap" style="display:none;position:absolute;bottom:0px;z-index:9999;width:100%;margin-top:-100px;font-family:PingFangSC-Regular, sans-serif;"><div id="read-more-mask" style="position: relative; height: 200px; background: -webkit-gradient(linear, 0 0%, 0 100%, from(rgba(255, 255, 255, 0)), to(rgb(255, 255, 255)));"></div> <a id="read-more-btn" target="_self" style="position: absolute; left: 50%; top: 70%; bottom: 30px; transform: translate(-50%, -50%); width: 160px; height: 36px; line-height: 36px; font-size: 15px; text-align: center; border: 1px solid rgb(222, 104, 109); color: rgb(222, 104, 109); background: rgb(255, 255, 255); cursor: pointer; border-radius: 6px;">阅读全文</a> <div id="btw-modal-wrap" style="display: none;"><div id="btw-mask" style="position: fixed; top: 0px; right: 0px; bottom: 0px; left: 0px; opacity: 0.7; z-index: 999; background: rgb(0, 0, 0);"></div> <div id="btw-modal" style="position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 300px; text-align: center; font-size: 13px; background: rgb(255, 255, 255); border-radius: 10px; z-index: 9999; font-family: PingFangSC-Regular, sans-serif;"><span id="btw-modal-close-btn" style="position: absolute; top: 5px; right: 15px; line-height: 34px; font-size: 34px; cursor: pointer; opacity: 0.2; z-index: 9999; color: rgb(0, 0, 0); background: none; border: none; outline: none;">×</span> <p id="btw-modal-header" style="margin-top: 40px; line-height: 1.8; font-size: 13px;">
                扫码或搜索：<span style="color: #E9405A; font-weight: bold;">冰河技术</span> <br>发送：<span id="fustack-token" class="token" style="color: #e9415a; font-weight: bold; font-size: 17px; margin-bottom: 45px;">290992</span> <br>即可<span style="color: #e9415a; font-weight: bold;">立即永久</span>解锁本站全部文章</p> <img src="/images/personal/qrcode.png" style="width: 180px; margin-top: 10px; margin-bottom: 30px; border: 8px solid rgb(230, 230, 230);"></div></div></div><div class="pay-read-more-wrap" style="display:none;position:absolute;bottom:0px;z-index:9999;width:100%;margin-top:-100px;font-family:PingFangSC-Regular, sans-serif;"><div id="pay-read-more-mask" style="position: relative; height: 200px; background: -webkit-gradient(linear, 0 0%, 0 100%, from(rgba(255, 255, 255, 0)), to(rgb(255, 255, 255)));"></div> <a id="pay-read-more-btn" target="_blank" style="position: absolute; left: 50%; top: 70%; bottom: 30px; transform: translate(-50%, -50%); width: 160px; height: 36px; line-height: 36px; font-size: 15px; text-align: center; border: 1px solid rgb(222, 104, 109); color: rgb(222, 104, 109); background: rgb(255, 255, 255); cursor: pointer; border-radius: 6px;">付费阅读</a></div></div></div>
    <script src="/assets/js/cg-styles.js?v=1653305936337" defer></script><script src="/assets/js/cg-4.js?v=1653305936337" defer></script><script src="/assets/js/cg-3.js?v=1653305936337" defer></script><script src="/assets/js/cg-49.js?v=1653305936337" defer></script><script src="/assets/js/cg-5.js?v=1653305936337" defer></script><script src="/assets/js/cg-6.js?v=1653305936337" defer></script><script src="/assets/js/cg-app.js?v=1653305936337" defer></script>
  </body>
</html>
